package top.appx.service.impl;

import org.springframework.stereotype.Service;
import top.appx.dao.EmailDao;
import top.appx.entity.EmailEntity;
import top.appx.helper.email.EmailHelper;
import top.appx.helper.email.EmailModel;
import top.appx.qo.EmailQO;
import top.appx.service.EmailService;
import top.appx.util.DateTimeUtil;
import top.appx.vo.EmailVO;
import top.appx.vo.Pager;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zwyhome on 2015/6/17.
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    private EmailDao emailDao;
    public void sendEmail(EmailEntity email) throws Exception {
        EmailModel em = new EmailModel(email.getTitle(),email.getContent(),email.getTarget());
        EmailHelper.sendEmail(em);
        email.setCreateTime(DateTimeUtil.now());
        emailDao.save(email);
    }

    public Pager<EmailVO> queryByPage(EmailQO bean, int page, int rows) {
        Pager<EmailEntity> pager = emailDao.queryByPage(bean,page,rows);
        Pager<EmailVO> pagerVO = new Pager<EmailVO>();
        List<EmailVO> list = new ArrayList<EmailVO>();
        for (EmailEntity a:pager.getRows()){
            EmailVO ev = new EmailVO(a);
            list.add(ev);
        }
        pagerVO.setRows(list);
        pagerVO.setTotal(pager.getTotal());
        return pagerVO;
    }
}
